package org.gp953.basic.controller;

import org.gp953.basic.domain.LoginDto;
import org.gp953.basic.exception.BusinessException;
import org.gp953.basic.service.ILoginService;
import org.gp953.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping// 后台登录
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){

        try {
            Map<String,Object> map = loginService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误！！");
        }
    }


    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody Map<String,String> map){

        try {// 微信登录业务需求，要在service层返回2个
            return loginService.wechatLogin(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("哦豁，系统崩溃了，我们正在殴打程序员！！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误！！");
        }
    }

    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String,String> map){

        try {// 微信登录业务需求，要在service层返回2个
            Map<String, Object> resultMap = loginService.binder(map);
            return AjaxResult.me().setResultObj(resultMap);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("哦豁，系统崩溃了，我们正在殴打程序员！！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误！！");
        }
    }
}
